/* 判断是否是修改商品 */
UP_MODIFY=false;
GoodsId=null;
isUpImg=true; //判断修改时是否修改了图片  true表示未修改  false表示修改了
imgUrl=null;
if(location.hash){
    UP_MODIFY=true;
    GoodsId=location.hash.split("#id=")[1];
    console.log(location.hash.split("#id="));
}
if(UP_MODIFY){
    var name1 = $("#shop_name").val().replace(/\s/g, "");
    var price = $("#shop_price").val().replace(/\s/g, "");
    var stock = $("#shop_stock").val().replace(/\s/g, "");
   
    console.log("修改---")
    //查询被该商品的信息填充到输入框中
    $.ajax({
        type: "get",
        url: url + "/shop/queryshopinfo.php",
        data: {
            id:GoodsId
        },
        dataType: "json",
        success: function (data) {
            console.log(data);
            if(data.state){
                imgUrl=data.goods.imagePath;
                $("#shop_name").val(data.goods.shopName);
                $("#shop_stock").val(data.goods.shopNum);
                $("#shop_price").val(data.goods.shopPrice);
                //设置商品状态单选按钮的状态
                if(data.goods.ShopState==1){
                    $("#shopRadio1").prop("checked",true);
                    $("#shopRadio2").prop("checked",false);
                }else{
                    $("#shopRadio1").prop("checked",false);
                    $("#shopRadio2").prop("checked",true);
                }
                //设置是否限购复选框的状态
                if(data.goods.islimit!=0){
                    $("#ifList").css("display","block");
                    $("#shopCheckbox").prop("checked",true);
                    $("#xingouNum").val(data.goods.limitNumber);
                }
                $("#up_shop_img_one").attr("src", url+data.goods.imagePath);
            }
        }
    });
   
}
/* 提交商品 */
$("#shopUpList").click(function () {
    switchDiv("shopUpBox")
})
        // 预览图片 
$("#img_file").on("change", function () {
    console.log("图片已更改")
    isUpImg=false;
    var objUrl = getObjectURL(this.files[0]);
    // 这句代码没什么作用，删掉也可以
    // console.log("objUrl = "+objUrl) ;
    if (objUrl) {
        // 在这里修改图片的地址属性
        $("#up_shop_img_one").attr("src", objUrl);
        console.log("图片已更改2")

    }

    function getObjectURL(file) {
        var url = null;
        // 下面函数执行的效果是一样的，只是需要针对不同的浏览器执行不同的 js 函数而已
        if (window.createObjectURL != undefined) { // basic
            url = window.createObjectURL(file);
        } else if (window.URL != undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL != undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }

})

$("#shop_up").click(function () {
    var name1 = $("#shop_name").val().replace(/\s/g, "");
    var price = $("#shop_price").val().replace(/\s/g, "");
    var stock = $("#shop_stock").val().replace(/\s/g, "");
    if (name1 == '' || price == '' || stock == '') {
        alert("请完善商品信息")
        return;
    }
    if (price < 0 || stock < 0) {
        alert("请填写正确的价格或库存")
        return;
    }
    var file = $("#img_file").get(0).files[0];
    console.log(file);
    var formData = new FormData();
    formData.append("filsde", file);
    $.ajax({
        type: "post",
        url: url + "/shop/imageupload.php",
        contentType: false,
        processData: false,
        data: formData,
        dataType: "json",
        success: function (data) {
            console.log(data);
            if (data.state || isUpImg) {
                var URL;
                var dataIF;
                var TYPE;
                if (!UP_MODIFY) {
                    //新增商品
                    TYPE="post";
                    URL= url + "/shop/addshop.php";
                    dataIF={
                        imagePath: data.imagepath,
                        shopName: name1,
                        shopNum: stock,
                        shopPrice: price,
                        ShopState: $("input[name='shop']:checked").val(),
                        islimit: $("input[name='xiangou']:checked").val() ? 1 : 0,
                        limitNumber:$("#xingouNum").val(),
                    }
                    console.log(dataIF);
                } else {
                    //修改商品
                    console.log("imgUrl==="+imgUrl)
                    TYPE="post";
                    URL= url + "/shop/updateShop.php";
                    dataIF={
                        id:GoodsId,
                        imagePath: isUpImg?imgUrl:data.imagepath,
                        shopName: name1,
                        shopNum: stock,
                        shopPrice: price,
                        ShopState: $("input[name='shop']:checked").val(),
                        islimit: $("input[name='xiangou']:checked").val() ? 1 : 0,
                        limitNumber:$("#xingouNum").val(),
                    }
                    console.log(dataIF);

                }
                $.ajax({
                    type: TYPE,
                    url:URL,

                    data: dataIF,
                    dataType: "json",
                    success: function (data) {
                       
                        console.log(data);
                
                        if( data.state &&  hint(data.msg)){
                            
                            window.location.href ="goodsList.html";
                        }
                    }
                });
            } else {
                alert("上传失败")
            }
            console.log("==========="+$("#xingouNum").val())
        }
    });

})


$("#xianGou").click(function(){
    console.log($("input[name='xiangou']:checked").val())
    console.log("==========="+$("#xingouNum").val())
    if($("input[name='xiangou']:checked").val()){
        $("#ifList").css("display","block");
    }else{
        $("#ifList").css("display","none");
    }
})
